import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mind_rubbish_type/queryResult.dart';
import 'package:mind_rubbish_type/utils/banner.dart';
import 'package:mind_rubbish_type/utils/search.dart';

import 'about.dart';

class Home extends StatefulWidget {
  Home({Key key}) : super(key: key);
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  ScrollController _listController = ScrollController();

  List bannerList = [
    {
      'url':
          'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2812244325,828590927&fm=15&gp=0.jpg',
      'sub': '垃圾分类人人有责'
    },
    {
      'url':
          'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1373747095,2366234022&fm=26&gp=0.jpg',
      'sub': '垃圾分类从我做起',
    },
    {
      'url':
          'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2871870379,2118121482&fm=15&gp=0.jpg',
      'sub': '垃圾分类利国利民',
    }
  ];

  List rubbishList = [
    {
      'type': 'chu',
      'name': '厨余垃圾',
      'sub': '',
    },
    {
      'type': 'hai',
      'name': '有害垃圾',
      'sub': '',
    },
    {
      'type': 'hui',
      'name': '可回收垃圾',
      'sub': '',
    },
    {
      'type': 'other',
      'name': '其他垃圾',
      'sub': '',
    }
  ];

  // banner 图
  List<BannerItem> initBanner(arr) {
    List<BannerItem> bannerList = [];
    List.generate(arr.length, (index) {
      bannerList.add(
          BannerItem.defaultBannerItem(arr[index]["url"], arr[index]["sub"]));
    });
    return bannerList;
  }

  // 垃圾菜单
  List<Widget> initRubbishList(arr) {
    List<Widget> rubbishList = [
      SizedBox(height: 10),
      Row(
        children: [
          SizedBox(width: 10),
          Padding(
              padding: EdgeInsets.only(top: 20, bottom: 20),
              child: Text(
                "垃圾类别简介：",
                maxLines: 5,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 22,
                  fontWeight: FontWeight.w700,
                ),
              ))
        ],
      ),
      SizedBox(height: 10),
    ];
    List.generate(arr.length, (index) {
      String type = arr[index]['type'];
      String name = arr[index]['name'];
      rubbishList.add(Padding(
        padding: EdgeInsets.only(bottom: 12),
        child: InkWell(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => About(type: type, name: name),
                ),
              );
            },
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                      color: Colors.grey[200],
                      offset: Offset(1.0, 2.0), //阴影xy轴偏移量
                      blurRadius: 1.0, //阴影模糊程度
                      spreadRadius: 1.0 //阴影扩散程度
                      ),
                ],
              ),
              padding: EdgeInsets.only(bottom: 15, left: 8, right: 8),
              width: MediaQuery.of(context).size.width / 2,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                      height: 200,
                      child: ClipRRect(
                        borderRadius: BorderRadius.all(Radius.circular(5)),
                        child: Image.asset('assets/images/${type}_type.png'),
                      )),
                  SizedBox(height: 5),
                  Container(
                    padding: EdgeInsets.only(left: 15, right: 15),
                    child: Column(
                      children: [
                        Text(
                          arr[index]['name'],
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 18,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            )),
      ));
    });
    return rubbishList;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "垃圾分类查询",
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            fontSize: 15,
          ),
        ),
      ),
      body: Container(
          width: MediaQuery.of(context).size.width,
          padding: EdgeInsets.only(top: 15),
          child: SingleChildScrollView(
            controller: _listController,
            child: Wrap(
              children: [
                Search(
                    goQueryResult: (val) => {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) =>
                                  QueryResult(keyword: val.toString()),
                            ),
                          )
                        }),
                Padding(
                  padding: EdgeInsets.only(left: 15, right: 15, top: 15),
                  child: new BannerWidget(
                    180.0,
                    initBanner(bannerList),
                    bannerPress: (pos, item) {
                      print('第 $item 点击了');
                    },
                  ),
                ),
                SizedBox(height: 15),
                ...initRubbishList(rubbishList)
              ],
            ),
          )),
    );
  }
}
